package kz.pompei.ifacer.visio.skin.paint;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.JComponent;
import javax.swing.JOptionPane;

import kz.pompei.ifacer.cursor_manager.def.DefRectGeo;
import kz.pompei.ifacer.grot.acts.Act;
import kz.pompei.ifacer.grot.acts.ChangeFieldsAct;
import kz.pompei.ifacer.model.CursorInfo;
import kz.pompei.ifacer.model.CursorKind;
import kz.pompei.ifacer.model.PaintKind;
import kz.pompei.ifacer.model.widgets.WidgetButton;
import kz.pompei.ifacer.visio.PaintUtil;
import kz.pompei.ifacer.visio.skin.SkinGetter;

public class PaintWidgetButton {
  
  private static final int R = 5;
  @SuppressWarnings("unused")
  private final SkinGetter skin;
  
  public PaintWidgetButton(SkinGetter skin) {
    this.skin = skin;
  }
  
  public void paint(Graphics2D g, WidgetButton w, PaintKind paintKind, Point mouse,
      Rectangle allSpace) {
    if (paintKind != PaintKind.MOVING) {
      g.setColor(new Color(0xD8D8D8));
      g.fillRoundRect(w.x, w.y, w.width, w.height, R, R);
    }
    
    if (paintKind.isOver()) {
      PaintUtil.drawOverRect(g, w.rect());
    } else {
      g.setColor(paintKind == PaintKind.MOVING ? Color.GRAY :Color.BLACK);
      g.drawRoundRect(w.x, w.y, w.width, w.height, R, R);
    }
    FontMetrics fm = g.getFontMetrics();
    
    if (w.caption != null) {
      int height = fm.getHeight();
      int width = fm.stringWidth(w.caption);
      
      int x = w.x + w.width / 2 - width / 2 + 3;
      int y = w.y + w.height / 2 + height / 2 - 4;
      Graphics2D g2 = (Graphics2D)g.create();
      g2.setColor(paintKind == PaintKind.MOVING ? Color.GRAY :Color.BLACK);
      g2.setClip(w.x, w.y, w.width, w.height);
      g2.drawString(w.caption, x, y);
      g2.dispose();
    }
    
    if (paintKind.isSel()) {
      PaintUtil.drawSelectionRect(g, w.x, w.y, w.width, w.height);
    }
  }
  
  public Rectangle getWidgetBound(Graphics2D g, WidgetButton w) {
    return w.rect();
  }
  
  public CursorInfo getCursorInfo(Graphics2D g, WidgetButton w, Point mouse) {
    {
      CursorKind cursorKind = getBorderCursor(w, mouse);
      if (cursorKind != null) {
        return new CursorInfo(cursorKind, null);
      }
    }
    if (w.rect().contains(mouse)) {
      return new CursorInfo(CursorKind.MOVE, "edit");
    }
    return null;
  }
  
  private CursorKind getBorderCursor(WidgetButton w, Point mouse) {
    return DefRectGeo.cursorKindOnBorder(mouse, w, PaintUtil.BORDER_WIDTH, PaintUtil.CORNER_SIZE);
  }
  
  public Act move(Graphics2D g, WidgetButton original, WidgetButton copy, Point fromPoint,
      Point toPoint) throws Exception {
    
    int dx = toPoint.x - fromPoint.x;
    int dy = toPoint.y - fromPoint.y;
    
    CursorKind borderCursor = getBorderCursor(original, fromPoint);
    
    if (borderCursor == null) borderCursor = CursorKind.MOVE;
    
    return PaintUtil.resize(dx, dy, borderCursor, original, copy);
  }
  
  public Act getDoubleClickAct(Graphics2D g, WidgetButton w, Point mouse, JComponent owner)
      throws Exception {
    if (!w.rect().contains(mouse)) return null;
    
    return changeCaption(w, owner);
  }
  
  private Act changeCaption(WidgetButton w, JComponent owner) {
    String newCaption = JOptionPane.showInputDialog(owner, "Укажите новое значение", w.caption);
    if (newCaption == null) return null;
    return new ChangeFieldsAct(w.uuid, "caption", newCaption);
  }
}
